﻿#include "VAOneXMLAdaptorAcousticsNCTMultipleNoiseWeight.h"

#include "FITKAcousticsPhysics/FITKAcousticsNCTMultipleNoise.h"
#include "rapidxml.hpp"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTLayUp.h"

namespace IO
{

    bool VAOneXMLAdaptorAcousticsNCTMultipleNoiseWeight::adaptR()
    {
        return readNCTMultipleNoiseWeight();
    }

    bool VAOneXMLAdaptorAcousticsNCTMultipleNoiseWeight::adaptW()
    {
        return writeNCTMultipleNoiseWeight();
    }

    bool VAOneXMLAdaptorAcousticsNCTMultipleNoiseWeight::readNCTMultipleNoiseWeight()
    {
        if (!_node)// 判断Fluid节点是否存在
        {
            return false;
        }

        bool b = true;
        //转换类型
        Acoustics::FITKAcousticsNCTMultipleNoiseWeight* layerData = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoiseWeight*>(_dataObj);

        if(!layerData)
        {
            return false;
        }

        double trimCoverage = readDoubleAttribute(_node, "trim_coverage", &b);

        if(b)
        {
            layerData->setCoverage(trimCoverage);
        }

        QString trimRefName = readStrAttribute(_node, "trim_ref", &b);

        if(b)
        {
            int id = getAcousticsObjectIdByName(trimRefName);
            layerData->setNCTID(id);
        }
        else
        {
            //读材料读取异常
            consoleMessage(3,"Read NCT Info Error" + trimRefName);
        }

        return true;
    }

    bool VAOneXMLAdaptorAcousticsNCTMultipleNoiseWeight::writeNCTMultipleNoiseWeight()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        //转换类型
        Acoustics::FITKAcousticsNCTMultipleNoiseWeight* layerData = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoiseWeight*>(_dataObj);

        if (!layerData)
        {
            return false;
        }

        writeAttribute(_node, "trim_coverage", layerData->getCoverage());
        //获取多层声学包或用户自定义声学包

        //获取全局数据管理器
        auto globalData = FITKAPP->getGlobalData();

        if(!globalData)
        {
            return false;
        }

        Acoustics::FITKAcousticsPhysics* physics = globalData->getPhysicsData<Acoustics::FITKAcousticsPhysics>();

        if (!physics)
        {
            return false;  // 如果转换失败，名称无法填写
        }

        auto acousticsNCTManager = physics->getFITKAcousticsNCTManager();//获取声包管理器

        if(!acousticsNCTManager)
        {
            return false;
        }

        auto NCTdata = acousticsNCTManager->getDataByID(layerData->getNCTID());

        if(!NCTdata)
        {
            return false;
        }

        writeAttribute(_node, "trim_ref", NCTdata->getDataObjectName());
        //数据写入成功
        return true;
    }

}
